/*******************************************************************************

  Paying Outsourced Labor: Direct Evidence from Linked Temp Agency-Worker-Client Data

  By Andres Drenik, Simon Jäger, Pascuel Plotkin and Benjamin Schoefer
	January 7th, 2021

	DESCRIPTION: Creates dataset of Random Group of Workers for Measurment
    Error Correction

*******************************************************************************/




/********************************************************************************
***** Preliminaries
********************************************************************************/
set more off
cap log close
local curr_date = c(current_date)
log using "${logs}/06_Workers_group`curr_date'", replace


/********************************************************************************
***** Load Clean Dataset
********************************************************************************/

use "${intermediate_data_emp}/full_Dataset_Clean.dta", clear


/****************************************************************************************
* Generate Random Groups
****************************************************************************************/

	*Keep only relevant variables
	keep cuil_trab date cuit_empl

	*Keep first observation for each worker
	bys cuil_trab (date): keep if _n == 1

	*Generate a runiform by firm to order the observations
	bys cuit_empl : gen rand = runiform()

	bys cuit_empl (rand) : gen number_obs = _n

	bys cuit_empl : gen total_obs = _N

	*We put half of the workers in each firm in one group and half in the other.
	gen group = (total_obs / 2 > number_obs)

	keep cuil_trab group

	save "${intermediate_data_emp}/workers_group.dta", replace

log close
